﻿using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JGSY.SerialCommunication.UpperComputer.Core.AOP.ThirdPartyLibrary
{
    /// <summary>
    /// 异常
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public class ExceptionCastleDynamicProxyAttribute : Attribute, IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            try
            {
                invocation.Proceed();
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine($"[异常处理] 参数验证失败: {ex.Message}");
                invocation.ReturnValue = "未知用户";
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[异常处理] 方法异常: {ex.Message}");
                Console.WriteLine($"[异常处理] 异常类型: {ex.GetType().FullName}");
                Console.WriteLine($"[异常处理] 堆栈跟踪: {ex.StackTrace}");

                // 可以添加日志记录或其他恢复操作
                // Logger.Error($"执行{invocation.Method.Name}时发生异常", ex);

                // 重新抛出异常或返回默认值
                //throw; 根据需要  throw可以继续抛出去
            }
            finally
            {

            }
        }
    }
}
